package cn.valo.basic.service.impl;

import cn.valo.basic.constants.BaseConstants;
import cn.valo.basic.dto.RegisterDto;
import cn.valo.basic.exception.BusinessException;
import cn.valo.basic.service.IVerifyCodeService;
import cn.valo.basic.util.SmsUtils;
import cn.valo.basic.util.StrUtils;
import cn.valo.basic.util.VerifyCodeUtils;
import cn.valo.user.domain.User;
import cn.valo.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Override
    public String imageCode(String key) {
        if(!StringUtils.hasLength(key)){
            throw new BusinessException("key不能为空！！！");
        }
//        (每次都是直接生成新的，但是要覆盖前面的)
//        。生成code = 9527
        String code = StrUtils.getComplexRandomString(4);
//        。保存redis，设置过期时间  key是什么==前端请求给我传递的UUID
        redisTemplate.opsForValue().set(
                key,//前端传递的UUID
                code, //图片中的码儿
                5,//过期时间
                TimeUnit.MINUTES);//过期时间的单位
//        。使用2D引擎把code画成图
//        。把图使用Base64编码成字符串
//        。返回Base64编码的字符串
        return VerifyCodeUtils.VerifyCode(100,38,code);

    }
    public void smsCode(RegisterDto dto){
        String phone=dto.getPhoneNumber();
        String imageCode=dto.getImageCode();
        String imageCodeKey=dto.getImageCodeKey();
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(imageCode)
                || StringUtils.isEmpty(imageCodeKey)){
            throw new BusinessException("参数异常！！！");
        }
        Object imageCodeTmp = redisTemplate.opsForValue().get(imageCodeKey);
        if(imageCodeTmp==null){
            throw  new BusinessException("图形验证码已失效，请重新获取！");
        }
        if(!imageCodeTmp.toString().equalsIgnoreCase(imageCode)){
            throw new BusinessException("图形验证码错误，请重新输入！");
        }
        User user = userMapper.queryByPhone(phone);
        if(user!=null){
            throw new BusinessException("手机号已被注册！请直接登录！");
        }
        String smsCode = "";
        String key = BaseConstants.RedisConstants.REGISTER_SMS_PREFIX + phone;
        Object smsCodeTmp = redisTemplate.opsForValue().get(key);
        if(smsCodeTmp!=null){
            if(redisTemplate.opsForValue().getOperations().getExpire(key)>120l){
                throw new BusinessException("爬！");
            }
            //大于1分钟但未过期
            smsCode = smsCodeTmp.toString();

        }else{//redis获取失败，生成新验证码
            smsCode = StrUtils.getComplexRandomString(4);

        }

        redisTemplate.opsForValue().set(
                key,
                smsCode,
                3,
                TimeUnit.MINUTES
        );
        //给用户发送短信验证码
//        try {
//            SmsUtils.sendSms(phone,"亲，您的验证码是："+smsCode+"。请在3分钟内使用！！");
//        } catch (Exception e) {
//            throw new BusinessException("发送验证码失败，请稍后重试！");
//        }
        System.out.println("亲，您的验证码是：" + smsCode + "。请在3分钟内使用！！");




    }
}
